package com.example.su_shi_tan_mine.child

import android.os.Bundle
import android.util.Log
import android.view.View
import com.alibaba.fastjson.JSON
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.base.BaseFragmentEx
import com.example.su_shi_tan_bas_lib.base.BaseLazyDataBindingFragment
import com.example.su_shi_tan_bas_lib.base.LazyFragment
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_core.extentions.goneAlphaAnimation
import com.example.su_shi_tan_core.extentions.visibleAlphaAnimation
import com.example.su_shi_tan_core.utils.clipboard
import com.example.su_shi_tan_mine.R
import com.example.su_shi_tan_mine.child.bean.SharePostAdvertiseBean
import com.example.su_shi_tan_mine.databinding.SharePostAdvertiseFragmentBinding

class SharePostAdvertiseFragment : BaseLazyDataBindingFragment<SharePostAdvertiseFragmentBinding>(),
    SharePostLabel, View.OnClickListener {

    companion object {
        fun newInstance(): SharePostAdvertiseFragment {
            val args = Bundle()
            val fragment = SharePostAdvertiseFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var mBean: SharePostAdvertiseBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }


    override fun labelInfo() = "分享海报"

    override fun layoutId() = R.layout.share_post_advertise_fragment

    override fun lazyInit() {
        RestClient.builder()
            .url(R.string.spread_banner)
            .success {
                Log.d("spread_banner", "lazyInit: $it")
                viewBinder.mLoading.goneAlphaAnimation()
                parseData(it)

            }
            .error { _, msg ->
                normalShortToast(_mActivity, msg)
                viewBinder.mProgressBar.goneAlphaAnimation()
                viewBinder.mReLoading.visibleAlphaAnimation()
            }.failure {
                normalShortToast(_mActivity, R.string.net_work_failed)
                viewBinder.mProgressBar.goneAlphaAnimation()
                viewBinder.mReLoading.visibleAlphaAnimation()
            }
            .build()
            .get()
    }

    private fun parseData(it: String?) {
        mBean = JSON.parseObject(it, SharePostAdvertiseBean::class.java)
        viewBinder.item = mBean
    }

    override fun initView(view: View) {
        viewBinder.onViewClick = this
        viewBinder.mLoading.visibleAlphaAnimation()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.mCopyTv -> {
                clipboard("", (mBean?.invite_url ?: ""))
            }
        }
    }


}